package com.mengma.ec.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.entity.User;
import com.mengma.ec.service.portal.IManagerOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Auther: xixi
 * @Date: 2018/10/8 10:12
 * @Description:
 */
@RestController
@RequestMapping(value = "/manage/order/")
public class ManagerOrderController {

    @Autowired
    private IManagerOrderService iManagerOrderService;

    @GetMapping("list.do")
    public ServerResponse<PageInfo> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        //1.2拿取用户的唯一角色信息 判断是否有管理员权限
        if(user.getRole() == Const.Role.ROLE_ADMIN){
            //去数据库查询数据 并返回ServerResponse<List<Category>>
            return  iManagerOrderService.list(user.getId(),pageNum,pageSize);
        }
        return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");
    }

    @GetMapping("search.do")
    public ServerResponse search(Long orderNo,HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iManagerOrderService.search(orderNo);
        return response;
    }

    @GetMapping("send_goods.do")
    public ServerResponse send_goods(Long orderNo,HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iManagerOrderService.send_goods(orderNo);
        return response;
    }
}
